﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;
using RapidWebDev.Common;

namespace RapidWebDev.SearchIndex.Contract
{
	/// <summary>
	/// Extended Linq DataContext which intercepts submitting entities for indexing.
	/// </summary>
	public abstract class IndexableDataContext : DataContext
	{
		/// <summary>
		/// Initializes a new instance of the System.Data.Linq.DataContext class by referencing the connection used by the .NET Framework.
		/// </summary>
		/// <param name="connection">The connection used by the .NET Framework.</param>
		public IndexableDataContext(IDbConnection connection) : base(connection) { }

		/// <summary>
		/// Initializes a new instance of the System.Data.Linq.DataContext class by referencing a file source.
		/// </summary>
		/// <param name="fileOrServerOrConnection">
		/// This argument can be any one of the following: The name of a file where a SQL Server Express database resides.
		/// The name of a server where a database is present.
		/// In this case the provider uses the default database for a user.
		/// A complete connection string. LINQ to SQL just passes the string to the provider without modification.
		/// </param>
		public IndexableDataContext(string fileOrServerOrConnection) : base(fileOrServerOrConnection) { }

		/// <summary>
		/// Initializes a new instance of the System.Data.Linq.DataContext class by referencing a connection and a mapping source.
		/// </summary>
		/// <param name="connection">The connection used by the .NET Framework.</param>
		/// <param name="mapping">The System.Data.Linq.Mapping.MappingSource.</param>
		public IndexableDataContext(IDbConnection connection, MappingSource mapping) : base(connection, mapping) { }

		/// <summary>
		/// Initializes a new instance of the System.Data.Linq.DataContext class by referencing a file source and a mapping source.
		/// </summary>
		/// <param name="fileOrServerOrConnection">
		/// This argument can be any one of the following: The name of a file where a SQL Server Express database resides.
		/// The name of a server where a database is present.
		/// In this case the provider uses the default database for a user.
		/// A complete connection string. LINQ to SQL just passes the string to the provider without modification.
		/// </param>
		/// <param name="mapping">The System.Data.Linq.Mapping.MappingSource.</param>
		public IndexableDataContext(string fileOrServerOrConnection, MappingSource mapping) : base(fileOrServerOrConnection, mapping) { }

		/// <summary>
		/// Sends changes that were made to retrieved objects to the underlying database, and specifies the action to be taken if the submission fails.
		/// </summary>
		/// <param name="failureMode">The action to be taken if the submission fails.</param>
		public override void SubmitChanges(ConflictMode failureMode)
		{
			IEntityIndexApi entityIndexApi = SpringContext.Current.GetObject<IEntityIndexApi>();
			ChangeSet changeSet = null;
			IEnumerable<object> entitiesToSave = null;
			IEnumerable<object> entitiesToDelete = null;
			if (entityIndexApi != null)
			{
				changeSet = this.GetChangeSet();
				entitiesToSave = changeSet.Inserts.Union(changeSet.Updates);
				entitiesToDelete = changeSet.Deletes;
			}

			base.SubmitChanges(failureMode);

			
			if (entityIndexApi != null)
			{
				entitiesToSave = changeSet.Inserts.Union(changeSet.Updates);
				foreach (object indexableEntity in entitiesToSave)
				{
					if (indexableEntity == null) continue;
					entityIndexApi.Save(indexableEntity);
				}

				entitiesToDelete = changeSet.Deletes;
				foreach (object indexableEntity in entitiesToDelete)
				{
					if (indexableEntity == null) continue;
					entityIndexApi.Delete(indexableEntity);
				}
			}
		}
	}
}